package com.ccp.dev.qbdms.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.poi.excel.Excel;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.extend.model.ExtendSysFile;
import com.ccp.dev.qbdms.consts.ZhiLiangConst;
import com.ccp.dev.qbdms.dao.WQbdChemicalPhysicsInfoDao;
import com.ccp.dev.qbdms.model.WQbdChemicalPhysicsInfo;
import com.ccp.dev.qbdms.util.BaseUtil;
import com.ccp.dev.qbdms.util.ProjectUUIDUtils;
import com.ccp.dev.qbdms.util.ReadAttachmentUtil;
import com.ccp.dev.system.dao.DictionaryDao;
import com.ccp.dev.system.dao.GlobalTypeDao;
import com.ccp.dev.system.model.Dictionary;
import com.ccp.dev.system.model.GlobalType;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 化学压力电源 信息操作服务类
 *
 * @author milihao
 * @date 2020-07-07 10:13:09
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WQbdChemicalPhysicsInfoService extends BaseService<WQbdChemicalPhysicsInfo> {

    @Resource
    private WQbdChemicalPhysicsInfoDao dao;
    @Resource
    private GlobalTypeDao globalTypeDao;
    @Resource
    private DictionaryDao dictionaryDao;


    public List<Dictionary> getSelectData(String typeValue, String secondTypeValue,
        String thirdTypeValue) {
        GlobalType globalType = globalTypeDao.getByDictNodeKey("zl");
        List<Dictionary> returnDictionList;
        List<Dictionary> dictionaryList = dictionaryDao.getDicByParentId(globalType.getTypeId());
        returnDictionList = dictionaryList;
        if (StringUtils.isNotBlank(typeValue)) {
            for (Dictionary dictionary : dictionaryList) {
                if (dictionary.getItemValue().equals(typeValue)) {
                    List<Dictionary> dictionaryList1 = dictionaryDao
                        .getDicByParentId(dictionary.getDicId());
                    returnDictionList = dictionaryList1;
                    if (StringUtils.isBlank(secondTypeValue)) {
                        break;
                    }
                    for (Dictionary dictionary1 : dictionaryList1) {
                        if (dictionary1.getItemValue().equals(secondTypeValue)) {
                            List<Dictionary> dictionaryList2 = dictionaryDao
                                .getDicByParentId(dictionary1.getDicId());
                            returnDictionList = dictionaryList2;
                            if (StringUtils.isBlank(thirdTypeValue)) {
                                break;
                            }
                            for (Dictionary dictionary2 : dictionaryList2) {
                                if (dictionary2.getItemValue().equals(thirdTypeValue)) {
                                    List<Dictionary> dictionaryList3 = dictionaryDao
                                        .getDicByParentId(dictionary2.getDicId());
                                    returnDictionList = dictionaryList3;
                                }
                            }

                        }
                    }
                }
            }
        }
        return returnDictionList;
    }


    public HSSFWorkbook exportExcel(Excel excel, QueryFilter queryFilter) {
        List<WQbdChemicalPhysicsInfo> dataList = this.dao.queryAll(queryFilter);
        int startRow = 1;
        //给每一个单元格赋值
        for (WQbdChemicalPhysicsInfo data : dataList) {
            excel.cell(startRow, 0).value(data.getId());
            //excel.cell(startRow, 1).value(data.getMc());
            //可在赋值时，根据需求进行格式化
            //excel.cell(startRow, 2).value(DateFormatUtil.formatDate(data.getRq()));
            //excel.cell(startRow, 3).value(data.getSj());
            //excel.cell(startRow, 4).value(data.getTenantId());
            startRow++;
        }
        return excel.getWorkBook();
    }

    public List<WQbdChemicalPhysicsInfo> reportList(String id, String tableName) {
        return dao.reportList(id, tableName);
    }

    public List<WQbdChemicalPhysicsInfo> queryAll(QueryFilter queryFilter) {
        return dao.queryAll(queryFilter);
    }

    public void addImportData(List<WQbdChemicalPhysicsInfo> list) {
        ZhiLiangConst.addCommonFields(list);
        //上报状态
        list.forEach(a -> {
            String mainId = ProjectUUIDUtils.getUUIDFor32();
            a.setId(mainId);
            a.setReportStatus(ZhiLiangConst.getDataInitStatusToHospital());
        });
        if (!list.isEmpty()) {
            dao.addList(list);
        }
    }

    /**
     * 根据主表id查询主表加子表数据
     *
     * @param id 主表id
     * @return
     */
    public WQbdChemicalPhysicsInfo getParentAndChilden(String id) {
        return dao.getParentAndChilden(id);
    }

    /**
     * 保存接口传输的数据
     *
     * @param wQbdChemicalPhysicsInfoList 主表model,有子表的包含子表数据
     * @return
     */
    public void saveAll(List<WQbdChemicalPhysicsInfo> wQbdChemicalPhysicsInfoList,List<ExtendSysFile> extendSysFileList){
        ReadAttachmentUtil.getEntityData(wQbdChemicalPhysicsInfoList,extendSysFileList);
        for (WQbdChemicalPhysicsInfo model : wQbdChemicalPhysicsInfoList) {
            // 如果上报数据中hospital为空,认为是三级上报来的,如果不为空,则认为是二级上报来的
            if (StringUtil.isEmpty(model.getFilldeptHospitalId())) {
                model.setBelongdeptname(BaseUtil.getCurrentTenOrgCodePath(model.getFilldeptid()));
            } else {
                model.setBelongdeptname(
                    BaseUtil.getCurrentTenOrgCodePath(model.getFilldeptHospitalId()));
            }
            if (null == dao.getById(model.getId())) {
                dao.add(model);
            } else {
                dao.update(model);
            }
        }
    }
}
